package com.mcxx.modules.miniSalvation.salary.service.read;

import cn.hutool.core.date.DateField;
import cn.hutool.core.date.DateUtil;
import com.mcxx.common.exception.BizException;
import com.mcxx.modules.miniSalvation.salary.dao.read.MemberSalaryReadDao;
import com.mcxx.modules.miniSalvation.salary.dto.FamilyMemberSalaryDTO;
import com.mcxx.modules.miniSalvation.salary.entity.MemberSalaryEntity;
import com.mcxx.util.FailureCode;
import com.yinhai.ta404.core.transaction.annotation.TaTransactional;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;


@Service
@TaTransactional(readOnly = true)
public class FamilyMemberSalaryReadService {
    @Autowired
    private MemberSalaryReadDao memberSalaryReadDao;



    
    public List<FamilyMemberSalaryDTO> getMemberHistorySalary(String familyId, String orderId, String areaCode){
        areaCode = areaCode.substring(0, 6);
        return memberSalaryReadDao.getMemberHistorySalary(familyId, orderId, areaCode);
    }

    
    public void checkMemberIsSaved(String orderId, String memberId, String startDate) {
        String nextMonth = DateUtil.parse(startDate, "yyyyMM").offset(DateField.MONTH,1).toString("yyyyMM");

        if (StringUtils.isEmpty(orderId)) {

            Integer existMemSalary = memberSalaryReadDao.queryMemberSalaryExist(memberId, nextMonth);
            if (existMemSalary > 0) {
                throw new BizException(FailureCode.ERR_20032);
            }
        } else {

            Integer existMemSalary = memberSalaryReadDao.getMemberSalaryExistByOrderId(memberId, nextMonth, orderId);
            if (existMemSalary > 0) {
                throw new BizException(FailureCode.ERR_20032);
            }
        }
    }

    
    public void checkMemberIsSaved(String memberId, String startDate) {
        String nextMonth = DateUtil.parse(startDate, "yyyyMM").offset(DateField.MONTH,1).toString("yyyyMM");


        Integer existMemSalary = memberSalaryReadDao.queryMemberSalaryExist(memberId, nextMonth);
        if (existMemSalary > 0) {

            throw new BizException(FailureCode.ERR_20032);
        }

    }


    public List<MemberSalaryEntity> queryMemberSumSalaryByOrderId(String id) {
        return memberSalaryReadDao.queryMemberSumSalaryByOrderId(id);
    }

    public List<MemberSalaryEntity> queryMemberItemSalaryByOrderId(String id) {
        return memberSalaryReadDao.queryMemberItemSalaryByOrderId(id);
    }

    
    public List<FamilyMemberSalaryDTO> getMemberSalary(String memberId, String businessType, String areaCode, String month){
        return memberSalaryReadDao.selectMemberSalaryByMemberId(memberId, businessType, areaCode, month);
    }

    
    public String getMemberSalaryMaxDate(String orderId, String memberId, String businessType){
        return memberSalaryReadDao.getMemberSalaryMaxDate(orderId, memberId, businessType);
    }

    
    public List<MemberSalaryEntity> queryMemberSalaryByOrderId(String id){
        return memberSalaryReadDao.queryMemberSalaryByOrderId(id);
    }

}
